package com.afdor.rws.dtm.persistence;

import com.afdor.rws.annotation.extension.SPI;
import com.afdor.rws.dtm.config.DtmConfig;
import com.afdor.rws.dtm.entities.DistributedTransactionDefinition;
import com.afdor.rws.dtm.exception.TransactionRuntimeException;
import com.afdor.rws.dtm.serialize.ObjectSerializer;

import java.util.List;

/**
 * 分布式事物持久化管理
 *
 * @author 悭梵
 * @date Created in 2018-10-15 14:18
 */
@SPI("default")
public interface DistributedTransactionPersistenceManager {

    /**
     * init.
     *
     * @param appName   rpc application name.
     * @param dtmConfig {@linkplain DtmConfig }
     * @throws Exception ex
     */
    void init(String appName, DtmConfig dtmConfig) throws Exception;


    /**
     * get scheme.
     *
     * @return scheme
     */
    String getScheme();


    /**
     * set objectSerializer spi.
     *
     * @param objectSerializer {@linkplain ObjectSerializer}
     */
    void setSerializer(ObjectSerializer objectSerializer);

    /**
     * save distributed transaction definition.
     *
     * @param transactionDefinition {@linkplain DistributedTransactionDefinition}
     * @return rows
     */
    int create(DistributedTransactionDefinition transactionDefinition);

    /**
     * delete distributed transaction definition.
     *
     * @param id pk
     * @return rows
     */
    int remove(String id);

    /**
     * update distributed transaction definition.
     *
     * @param transactionDefinition {@linkplain DistributedTransactionDefinition}
     * @return rows
     * @throws TransactionRuntimeException ex
     */
    int update(DistributedTransactionDefinition transactionDefinition) throws TransactionRuntimeException;

    /**
     * find distributed transaction definition by id.
     *
     * @param id pk
     * @return {@linkplain DistributedTransactionDefinition}
     */
    DistributedTransactionDefinition findById(String id);

    /**
     * find distributed transaction definition by id.
     *
     * @param groupId main transaction id
     * @return {@linkplain DistributedTransactionDefinition}
     */
    List<DistributedTransactionDefinition> findByGroupId(String groupId);
}
